package com.atguigu.chapter07.a_window;

import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * @ClassName: Flink01_Tumbling_Window
 * @Description:
 * @Author: kele
 * @Date: 2021/4/6 20:06
 *
 * 解决reduce之后，数据类型必须和输入的数据类型一致的问题
 *
 **/
public class Flink08_Window_Aggregate_Function {

    public static void main(String[] args) {

        Configuration conf = new Configuration();
        conf.setInteger("rest.port",20000);

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(2);

        DataStreamSource<String> ds = env.socketTextStream("hadoop162", 8888);

        ds.flatMap(new FlatMapFunction<String, Tuple2<String,Long>>() {
            @Override
            public void flatMap(String line, Collector<Tuple2<String, Long>> out) throws Exception {

                for (String word : line.split(" ")) {
                    out.collect(Tuple2.of(word,1l));
                }
            }
        })
                .keyBy(d -> d.f0)
                .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
                .aggregate(new AggregateFunction<Tuple2<String, Long>, Long, Long>() {
                               //第一个元素进入时初始化
                               @Override
                               public Long createAccumulator() {
                                   System.out.println("create");
                                   return 0l;
                               }

                               //每进来一个元素触发一次
                               @Override
                               public Long add(Tuple2<String, Long> value, Long accumulator) {
                                   System.out.println("add...");
                                   return accumulator + 1;
                               }

                               //将结果返回到之后的算子中
                               @Override
                               public Long getResult(Long accumulator) {
                                   return accumulator;
                               }

                               //合并两个类的计算值，只有在session中使用
                               @Override
                               public Long merge(Long a, Long b) {
                                   return a + b;
                               }
                           },
                        new WindowFunction<Long, String, String, TimeWindow>() {

                            @Override
                            public void apply(String s,
                                              TimeWindow window,
                                              Iterable<Long> input,
                                              Collector<String> out) throws Exception {

                                Long next = input.iterator().next();

                                out.collect("最终结果为:"+next);
                            }
                        }).print();
                


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}
